package com.ossean;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.ossean.databaseSource.DBSource;
import com.ossean.model.OpenSourceProject;


@Component
public class ChangeSynonyms {
	
	Logger logger = Logger.getLogger(this.getClass());
	
	@Resource
	private DBSource dbSource;
	
	private static String sourceTableName = "open_source_projects";
	private static String targetTableName = "synonyms";
	private static String pointerTableName = "edd_pointers";
	
	private static int batchSize = 1000;
	
	
	//读指针
    public int readPointer(String table, String source, String target){
		int pointer = 1;
		//pointer的初始值应该从数据库中读出最小的id
		try {
			pointer = dbSource.getMinId(source);
		} catch (Exception e){
			//表示表中没有数据
			logger.info("No item in this table");
			dbSource.insertPointer(table, source, target, 1);
			return 1;
		}
		
		try {
			pointer = dbSource.getPointer(table, source, target);
		} catch(Exception e) {
			logger.info("No such pointer! Create one");
			dbSource.insertPointer(table, source, target, pointer);
		}
		return pointer;
	}
    
    
	public void start(){
		while(true){
			//读取对应的指针
			int startId = readPointer(pointerTableName, sourceTableName, targetTableName);
			List<OpenSourceProject> openSourceProjectList = dbSource.getOpenSourceProjectList(sourceTableName, startId, batchSize);
			
			if(openSourceProjectList.size() == 0){
				//表示程序执行完成
				logger.info("程序执行完成");
				System.exit(0);
			}
			
			for(OpenSourceProject project:openSourceProjectList){
				String synonyms = project.getSynonyms();
				logger.info("处理项目:" + project.getId());
				//判断synonyms是否需要修改
				if(synonyms == null || "".equals(synonyms)){
					dbSource.updatePointer(pointerTableName, sourceTableName, targetTableName, project.getId() + 1); //更新指针
					continue; //表示没有同义词 不需要修改
				}
				
				if(synonyms.indexOf("<") >= 0){
					dbSource.updatePointer(pointerTableName, sourceTableName, targetTableName, project.getId() + 1); //更新指针
					continue; //表示存储的是正确的结果 不需要修改
				}else{
					logger.info("修改项目:" + project.getId());
					String result = "";
					String[] array = synonyms.split(",");
					for(String s:array){
						result = result + "<" + s + ">,";
					}
					result = result.substring(0, result.length() - 1);
					dbSource.updateSynonyms(sourceTableName, result, project.getId());
					dbSource.updatePointer(pointerTableName, sourceTableName, targetTableName, project.getId() + 1); //更新指针
				}
			}
		}
		
	}
	
	
	public static void main(String[] args){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/applicationContext*.xml");
		ChangeSynonyms Main = applicationContext.getBean(ChangeSynonyms.class);
		Main.start();
	}
}
